/**
 * respons js file;
 * company:junqitecch
 * author:xuzuiotao
 * handling response information
 */

var  consts  = require('../consts');
var  tournote = require('../mongo/tournote');
var cache=require('../redis/usercache');



/*
 * 获取游迹列表，
 */
function processList(request,response,callback){

	var data =request.data;
    
	var count =20;
	var baseLine=0;
	var direction = -1;
	var param = null;
    try {
        param = JSON.parse(data);    	
	} catch (e) {
		
	}
	
	var userID=0;
	if(param!==null){
		if(param.baseLine !==null && typeof(param.baseLine)!=='undefined'){
			baseLine = Number(param.baseLine);
		}
		if(param.count !==null && typeof(param.count)!=='undefined'){
			count = Number(param.count);
		}	
		if(param.direction !==null && typeof(param.direction)!=='undefined'){
			direction = Number(param.direction);
		}
		if(param.userID !==null && typeof(param.userID)!=='undefined'){
			userID = Number(param.userID);
		}
	}

	/*
	 *     ID:
           userID:
           title:
           coverURL:
           followedNum:
           commentedNum:
           praisedNum:
           content:[{},{}];
           createTime:
           updateTime:
           latestTime:
           items:[{}]
	 * 
	 */
	
//	TEXT:3
//	text:aaa
//	type=3;
//	
	
	console.log(userID+'');
	 if(userID<=0){
		 tournote.listTourNote(baseLine,direction,count,function (error,obj){
		    	if(error !==null){
		    		response.errorCode=error.number;
					response.message= error.message;
					if(callback){
						callback(response);
					}
					return;
		    		
		    	}
		    	
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data = JSON.stringify(obj);	
				if(callback){
					callback(response);
				}
		 });
		 
	 }else{

	    	 tournote.listTourNoteByUserID(userID, baseLine,direction,count,function (error,obj){
		    	if(error !==null){
		    		response.errorCode=error.number;
					response.message= error.message;
					if(callback){
						callback(response);
					}
					return;
		    		
		    	}
		    	
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data = JSON.stringify(obj);	
				if(callback){
					callback(response);
				}
		 });
	
	 }
	
	
}

/*
 * 修改游迹相关内容
 */
function processUpdate(request,response,callback){
	/*
	 * 修改游迹目前不实现
	 */	
}

/*
 * 删除游迹
 */
function processDelete(request,response,callback){
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param._id ===null || typeof(param._id)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':_id';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		tournote.deleteTourNote(entity.userID,param._id,function (error,obj){
		    if(error !==null){
		    	response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
		    }
		    	
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			if(callback){
				callback(response);
			}
		 });
	
	});

	
	
}

/*
 * 发布游迹
 */
function processPublish(request,response,callback){
	console.log('processPublish111');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	var param = null;
    try {
        param = JSON.parse(data);    	
	} catch (e) {
		
	}
	
	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	/*
	 *     ID:
           userID:
           title:
           coverURL:
           followedNum:
           commentedNum:
           praisedNum:
           content:[{},{}];
           createTime:
           updateTime:
           latestTime:
           items:[{}]
	 * 
	 */
	
//	TEXT:3
//	text:aaa
//	type=3;
//	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		console.log(entity);
	    tournote.insertTourNote(entity,param,function (error,obj){
	    	if(error !==null){
	    		response.errorCode=error.number;
				response.message= error.message;
				if(callback){
					callback(response);
				}
				return;
	    		
	    	}
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);	
			if(callback){
				callback(response);
			}
	    });

	});
	
}

/*
 * 获取我的游迹
 */
function processMine(request,response,callback){
	console.log('processMine');
	
	var data =request.data;
	if(request.token===null || typeof(request.token)==='undefined'){
		response.errorCode=consts.Error.NEED_LOGIN;
		response.message=consts.Message.NEED_LOGIN;
		if(callback){
			callback(response);
		}
		return;
	}
	
	
	cache.getByToken(request.token,function(tk,entity){
		if(tk===null | typeof(tk)==='undefined'){
			response.errorCode=consts.Error.NEED_LOGIN;
			response.message=consts.Message.NEED_LOGIN;
			if(callback){
				callback(response);
			}
			return;
		}
		var data =request.data;
	    
		var count =20;
		var baseLine=0;
		var direction = -1;
		var param = null;
	    try {
	        param = JSON.parse(data);    	
		} catch (e) {
			
		}
		
		if(param!==null){
			if(param.baseLine !==null && typeof(param.baseLine)!=='undefined'){
				baseLine = param.baseLine;
			}
			if(param.count !==null && typeof(param.count)!=='undefined'){
				count = param.count;
			}	
			if(param.direction !==null && typeof(param.direction)!=='undefined'){
				direction = param.direction;
			}
		}

		
		 tournote.listTourNoteByUserID(entity.userID, baseLine,direction,count,function (error,obj){
		    	if(error !==null){
		    		response.errorCode=error.number;
					response.message= error.message;
					if(callback){
						callback(response);
					}
					return;
		    		
		    	}
		    	
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data = JSON.stringify(obj);	
				if(callback){
					callback(response);
				}
		 });
	
	});
	
}



function processDetail(request,response,callback){
	console.log('processDetail');

	var data =request.data;

	
	if(data===null || typeof(data)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;

	}else {
		console.log(data);
	}
    var param = null;
    
    try {
    	param = JSON.parse(data);
	} catch (e) {
	}

	if(param===null){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM;
		if(callback){
			callback(response);
		}
		return false;
	}

	if(param._id ===null || typeof(param._id)==='undefined'){
		response.errorCode=consts.Error.ERROR_PARAM;
		response.message=consts.Message.ERROR_PARAM+':_id';
		if(callback){
			callback(response);
		}
		return false;
	}
	
	
	if(request.token===null || typeof(request.token)==='undefined'){
		tournote.detailTourNote(param._id,0,function(err,obj){
			
			if(err){
				response.errorCode = err.number;
				response.message= err.message;
				if(callback){
					callback(response);
				}
				return;
			}
			
			response.errorCode = consts.Error.NONE_ERROR;
			response.message= consts.Message.OK_MESSAGE;
			response.data = JSON.stringify(obj);	
			if(callback){
				callback(response);
			}
		});
	}else{
		cache.getByToken(request.token,function(tk,entity){
			var userID=0;
			if(entity){
				userID=entity.userID;

			}

			tournote.detailTourNote(param._id,userID,function(err,obj){
				
				if(err){
					response.errorCode = err.number;
					response.message= err.message;
					if(callback){
						callback(response);
					}
					return;
				}
				response.errorCode = consts.Error.NONE_ERROR;
				response.message= consts.Message.OK_MESSAGE;
				response.data = JSON.stringify(obj);	
				if(callback){
					callback(response);
				}
			});
			
		});
	}
	
	
}



module.exports={
	processList:processList,
	processUpdate:processUpdate,
	processDelete:processDelete,
	processPublish:processPublish,
	processMine:processMine,	
	processDetail:processDetail,
};


